package com.middlegroup.subjectpay.service.impl;

import com.middlegroup.common.enums.ChannelCode;
import com.middlegroup.common.enums.ServiceType;
import com.middlegroup.common.exception.ErrorMsg;
import com.middlegroup.common.model.SubjectpayQueryRequest;
import com.middlegroup.common.model.TransResult;
import com.middlegroup.subjectpay.entity.SubjectInBills;
import com.middlegroup.subjectpay.entity.SubjectOutBills;
import com.middlegroup.subjectpay.entity.ThirdProtocolBindRecord;
import com.middlegroup.subjectpay.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *
 * @author pc
 * @date 2019/1/14
 */
@Service
@Slf4j
public class QueryServiceImpl implements QueryService {
    @Resource
    private SubjectOutBillsService subjectOutBillsService;
    @Resource
    private SubjectInBillsService subjectInBillsService;
    @Resource
    private ChangpayService changpayService;
    @Resource
    private HelipayService helipayService;
    @Resource
    private KjtpayService kjtpayService;
    @Resource
    private UcfpayService ucfpayService;
    @Resource
    private ThirdProtocolBindRecordService thirdProtocolBindRecordService;

    @Override
    public TransResult orderQuery(SubjectpayQueryRequest request) {
        TransResult transResult = new TransResult();
        transResult.setBillNo(request.getBillNo());
        transResult.setMerchantBillNo(request.getMerchantBillNo());
        transResult.setTransStatus(0);
        transResult.setTransTime(System.currentTimeMillis());

        if(StringUtils.equals(ServiceType.DFQUERY.name(),request.getServiceType())){
            //TODO 付款的查询
            SubjectOutBills subjectOutBillsFind=new SubjectOutBills();
            subjectOutBillsFind.setSubjectCode(request.getSubjectCode());
            subjectOutBillsFind.setMerchantBillNo(request.getMerchantBillNo());
            subjectOutBillsFind.setBillNo(request.getBillNo());
            SubjectOutBills subjectOutBills=subjectOutBillsService.getLastOneOutBills(subjectOutBillsFind);

            if(null==subjectOutBills){
                transResult.setMsg(ErrorMsg.ORDER_NOT_FOUND);
                return transResult;
            }

            transResult.setAmount(subjectOutBills.getAmount());
            request.setChannelCode(subjectOutBills.getChannelCode());
            request.setAmount(subjectOutBills.getAmount());

            /**
             * 此处一定要注意，调用第三方渠道接口必须返回TransResult，不可为空，且保证异常在接口中已处理
             */
            ChannelCode channelCode=ChannelCode.valueOf(subjectOutBills.getChannelCode());
            switch (channelCode){
                case CH0000001:
                    try{
                        //TODO 调用第三方通道接口：快捷通
                        transResult=kjtpayService.dfQuery(request);
                    }catch (Exception e){
                        log.error("CH000001第三方通道交易查询接口调用失败"+request.getBillNo(),e);
                    }
                    break;
                case CH0000002:
                    try{
                        //TODO 调用第三方通道接口：合利宝
                        transResult=helipayService.dfQuery(request);
                    }catch (Exception e){
                        log.error("CH000002第三方通道交易查询接口调用失败"+request.getBillNo(),e);
                    }
                    break;
                case CH0000003:
                    try{
                        //TODO 调用第三方通道接口：畅捷支付
                        transResult=changpayService.dfQuery(request);
                    }catch (Exception e){
                        log.error("CH000003第三方通道交易查询接口调用失败"+request.getBillNo(),e);
                    }
                    break;
                case CH0000004:
                    try{
                        //TODO 调用第三方通道接口：合利宝支付D1
                        transResult=helipayService.dfQuery(request);
                    }catch (Exception e){
                        log.error("CH000003第三方通道交易接口调用失败"+request.getBillNo(),e);
                    }
                    break;
                case CH0000005:
                    try{
                        //TODO 调用第三方通道接口：畅捷支付D1
                        transResult=changpayService.dfQuery(request);
                    }catch (Exception e){
                        log.error("CH000003第三方通道交易查询接口调用失败"+request.getBillNo(),e);
                    }
                    break;
                case EMPTY0000001:
                    //TODO 空渠道查询
                    transResult = new TransResult(request.getBillNo(),request.getMerchantBillNo(),1,"模拟交易成功!",System.currentTimeMillis());
                    break;
                default:
                    transResult.setTransStatus(0);
                    transResult.setMsg("未找到相应channelCode!");
            }

            //是否更新bills表状态
            boolean ifUpdateBillStatus = null != transResult && null != transResult.getTransStatus() && (transResult.getTransStatus().intValue()==1 || transResult.getTransStatus().intValue()==2);
            if (ifUpdateBillStatus){
                subjectOutBills.setStatus(transResult.getTransStatus());
                subjectOutBillsService.updateById(subjectOutBills);
            }


        }else if(StringUtils.equals(ServiceType.DSQUERY.name(),request.getServiceType())){
            //TODO 扣款的查询
            SubjectInBills subjectInBillsFind=new SubjectInBills();
            subjectInBillsFind.setSubjectCode(request.getSubjectCode());
            subjectInBillsFind.setMerchantBillNo(request.getMerchantBillNo());
            subjectInBillsFind.setBillNo(request.getBillNo());
            SubjectInBills subjectInBills=subjectInBillsService.getLastOneInBills(subjectInBillsFind);

            if(null==subjectInBills){
                transResult.setMsg(ErrorMsg.ORDER_NOT_FOUND);
                return transResult;
            }

            transResult.setAmount(subjectInBills.getAmount());
            request.setChannelCode(subjectInBills.getChannelCode());
            request.setAmount(subjectInBills.getAmount());

            ChannelCode channelCode=ChannelCode.valueOf(subjectInBills.getChannelCode());
            switch (channelCode){
                case CH1111111:
                    try{
                        //TODO 调用第三方通道接口：畅捷
                        transResult=changpayService.deductQuery(request);
                    }catch (Exception e){
                        log.error("CH1111111第三方通道交易查询接口调用失败"+request.getBillNo(),e);
                    }
                    break;
                case CH1111112:
                    try{
                        //TODO 调用第三方通道接口：畅捷D1
                        transResult=changpayService.deductQuery(request);
                    }catch (Exception e){
                        log.error("CH1111112第三方通道交易查询接口调用失败"+request.getBillNo(),e);
                    }
                    break;
                case CH3333331:
                    try{
                        //TODO 调用第三方通道接口：先锋协议支付
                        transResult=ucfpayService.protocolPayQuery(request);
                    }catch (Exception e){
                        log.error("CH3333331第三方通道交易查询接口调用失败"+request.getBillNo(),e);
                    }
                    break;
                case CH3333332:
                    try{
                        //TODO 调用第三方通道接口：快捷通协议支付
                        transResult=kjtpayService.protocolPayQuery(request);
                    }catch (Exception e){
                        log.error("CH3333332第三方通道交易查询接口调用失败"+request.getBillNo(),e);
                    }
                    break;
                case CH3333333:
                    try{
                        //TODO 调用第三方通道接口：畅捷协议支付
                        transResult=changpayService.protocolPayQuery(request);
                    }catch (Exception e){
                        log.error("CH3333333第三方通道交易查询接口调用失败"+request.getBillNo(),e);
                    }
                    break;
                case CH2222221:
                    try{
                        //TODO 调用第三方通道接口：先锋快捷支付
                        transResult=ucfpayService.quickPayQuery(request);
                    }catch (Exception e){
                        log.error("CH2222221第三方通道交易查询接口调用失败"+request.getBillNo(),e);
                    }
                    break;
                case EMPTY0000002:
                    //TODO 空渠道查询
                    transResult = new TransResult(request.getBillNo(),request.getMerchantBillNo(),1,"模拟交易成功!",System.currentTimeMillis());
                    break;
                case CH0000006:
                    try{
                        //TODO 调用第三方通道接口：合利宝支付代扣
                        transResult = helipayService.deductQuery(request);
                    }catch (Exception e){
                        log.error("CH0000006第三方通道交易查询接口调用失败：{}{}",request.getBillNo(),e);
                    }
                    break;
                default:
                    transResult.setTransStatus(0);
                    transResult.setMsg("未找到相应channelCode!");
            }

            //是否更新bills表状态
            boolean ifUpdateBillStatus = null != transResult && null != transResult.getTransStatus() && (transResult.getTransStatus().intValue()==1 || transResult.getTransStatus().intValue()==2);
            if (ifUpdateBillStatus){
                subjectInBills.setStatus(transResult.getTransStatus());
                subjectInBillsService.updateById(subjectInBills);
            }

        }else if(StringUtils.equals(ServiceType.BINDCARDQUERY.name(),request.getServiceType())){
            //TODO 绑卡的查询，查询绑卡处理中的单子(本地数据库的查询支付中台有记录)
            ThirdProtocolBindRecord thirdProtocolBindRecordFind=new ThirdProtocolBindRecord();
            thirdProtocolBindRecordFind.setSubjectCode(request.getSubjectCode());
            thirdProtocolBindRecordFind.setMerchantBillNo(request.getMerchantBillNo());
            thirdProtocolBindRecordFind.setBillNo(request.getBillNo());
            ThirdProtocolBindRecord thirdProtocolBindRecord=thirdProtocolBindRecordService.getLastOne(thirdProtocolBindRecordFind);

            if(null==thirdProtocolBindRecord){
                transResult.setMsg(ErrorMsg.ORDER_NOT_FOUND);
                return transResult;
            }

            ChannelCode channelCode=ChannelCode.valueOf(thirdProtocolBindRecord.getChannelCode());
            switch (channelCode){
                case CH3333331:
                    try{
                        //TODO 调用第三方通道接口：先锋协议支付绑卡结果查询
                        transResult=ucfpayService.bindcardQuery(request);
                    }catch (Exception e){
                        log.error("CH3333331第三方通道交易查询接口调用失败"+request.getBillNo(),e);
                    }
                    break;
                default:
                    transResult.setTransStatus(0);
                    transResult.setMsg("未找到相应channelCode!");
            }
        }else {
            transResult.setTransStatus(0);
            transResult.setMsg("未找到对应的服务类型!");
        }


        return transResult;
    }
}
